package cn.yuanqiao.web.controller.archive.danganzhengli;

import cn.yuanqiao.archive.danganzhengli.domain.HandOverReceive;
import cn.yuanqiao.archive.danganzhengli.service.IHandOverReceiveService;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author zjj
 * @date 2023/7/22
 */
@RestController
@RequestMapping("/archive/yijiaoDan")
public class HandOverReceiveController extends BaseController {

    @Autowired
    private IHandOverReceiveService handOverReceiveService;

    /**
     * 添加移交接收单
     * @param handOverReceive
     * @return
     */
    @Log(title = "添加移交接收单", businessType = BusinessType.INSERT)
    @PostMapping("/insertHandOverReceive")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:insertHandOverReceive')")
    public AjaxResult insertHandOverReceive(@RequestBody HandOverReceive handOverReceive){
        handOverReceiveService.insertHandOverReceive(handOverReceive);
        return AjaxResult.success();
    }
    /**
     * 添加移交接收单
     * @param handOverReceive
     * @return
     */
    @Log(title = "添加移交接收单", businessType = BusinessType.INSERT)
    @PostMapping("/insertNewHandOverReceive")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDanTi:insertHandOverReceive')")
    public AjaxResult insertNewHandOverReceive(@RequestBody HandOverReceive handOverReceive){
        handOverReceiveService.insertNewHandOverReceive(handOverReceive);
        return AjaxResult.success();
    }
    /**
     * 查询移交单列表
     * @return
     */
    @Log(title = "查询移交单列表", businessType = BusinessType.SELECT)
    @PostMapping("/selectHandOverReceive")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:selectHandOverReceive')")
    public TableDataInfo selectHandOverReceive(@Param("name")String name, @Param("state")Integer state, @Param("yijiaoDate")String yijaoDate, @Param("type")String type)throws IOException {
        HandOverReceive handOverReceive = new HandOverReceive();
        handOverReceive.setName(name);
        handOverReceive.setState(state); // state = -1 时查询 state in (0,1,2,99)
        handOverReceive.setCreateTime(yijaoDate);
        handOverReceive.setType(type);
        startPage();
        List<HandOverReceive> list = handOverReceiveService.selectHandOverReceive(handOverReceive);
        return getDataTable(list);
    }

    /**
     * 查询移交单列表
     * @return
     */
    @Log(title = "查询移交单列表", businessType = BusinessType.SELECT)
    @PostMapping("/selectHandOverReceive1")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:selectHandOverReceive1')")
    public TableDataInfo selectHandOverReceive1(@Param("name")String name, @Param("state")Integer state, @Param("yijiaoDate")String yijaoDate, @Param("type")String type)throws IOException {
        HandOverReceive handOverReceive = new HandOverReceive();
        handOverReceive.setName(name);
        handOverReceive.setState(state); // state = -1 时查询 state in (0,1,2,99)
        handOverReceive.setCreateTime(yijaoDate);
        handOverReceive.setType(type);
        startPage();
        List<HandOverReceive> list = handOverReceiveService.selectHandOverReceive(handOverReceive);
        return getDataTable(list);
    }

    /**
     * 查询单个移交单
     * @param id
     * @return
     */
    @Log(title = "查询单个移交单", businessType = BusinessType.SELECT)
    @PostMapping("/selectHandOverReceiveById")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:selectHandOverReceiveById')")
    public AjaxResult selectHandOverReceiveById(@Param("id")Long id){
        HandOverReceive handOverReceive = new HandOverReceive();
        handOverReceive.setId(id);
        HandOverReceive data = handOverReceiveService.selectHandOverReceiveById(handOverReceive);
        return AjaxResult.success(data);
    }

    /**
     * 查询单个移交单
     * @param id
     * @return
     */
    @Log(title = "查询单个移交单", businessType = BusinessType.SELECT)
    @PostMapping("/selectHandOverReceiveById1")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:selectHandOverReceiveById1')")
    public AjaxResult selectHandOverReceiveById1(@Param("id")Long id){
        HandOverReceive handOverReceive = new HandOverReceive();
        handOverReceive.setId(id);
        HandOverReceive data = handOverReceiveService.selectHandOverReceiveById(handOverReceive);
        return AjaxResult.success(data);
    }

    /**
     * 获取文件数
     * @return
     */
    @Log(title = "获取文件数", businessType = BusinessType.SELECT)
    @PostMapping("/getFileCount")
    public AjaxResult getFileCount(@RequestParam("categoryId")Long categoryId){
        int efileCount = handOverReceiveService.getFileCount(categoryId);
        return AjaxResult.success(efileCount);
    }

    /**
     * 获取文件大小
     * @return
     */
    @Log(title = "获取文件大小", businessType = BusinessType.SELECT)
    @PostMapping("/getFileSize")
    public AjaxResult getFileSize(@RequestParam("categoryId")Long categoryId){
        String efileCount = handOverReceiveService.getFileSize(categoryId);
        return AjaxResult.success(efileCount);
    }

    /**
     * 下单移交单
     * @return
     */
    @Log(title = "下单移交单Xlsx", businessType = BusinessType.EXPORT)
    @PostMapping("/exportXlsx")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:exportXlsx')")
    public void exportXlsx(@RequestParam("id") Long id, HttpServletResponse response)throws IOException {
        List<HandOverReceive> list = handOverReceiveService.exportData(id);
        ExcelUtil<HandOverReceive> util = new ExcelUtil<HandOverReceive>(HandOverReceive.class);
        util.exportExcel(response, list, "档案接收登记记录"+String.valueOf(new Date().getTime()));
    }

    /**
     * 下单移交单
     * @return
     */
    @Log(title = "下单移交单Xlsx", businessType = BusinessType.EXPORT)
    @PostMapping("/exportXlsx1")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:exportXlsx1')")
    public void exportXlsx1(@RequestParam("id") Long id, HttpServletResponse response)throws IOException {
        List<HandOverReceive> list = handOverReceiveService.exportData(id);
        ExcelUtil<HandOverReceive> util = new ExcelUtil<HandOverReceive>(HandOverReceive.class);
        util.exportExcel(response, list, "档案接收登记记录"+String.valueOf(new Date().getTime()));
    }

    /**
     * 下单移交单
     * @return
     */
    @Log(title = "下单移交单Word", businessType = BusinessType.EXPORT)
    @PostMapping("/exportWord")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:exportWord')")
    public void exportWord(@RequestParam("id") Long id, HttpServletResponse response)throws IOException {
        handOverReceiveService.exportWord(id,response);
    }

    /**
     * 下单移交单
     * @return
     */
    @Log(title = "下单移交单Word", businessType = BusinessType.EXPORT)
    @PostMapping("/exportWord1")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:exportWord1')")
    public void exportWord1(@RequestParam("id") Long id, HttpServletResponse response)throws IOException {
        handOverReceiveService.exportWord(id,response);

    }

    /**
     * 下载移交包
     * @return
     */
    @Log(title = "下载移交包", businessType = BusinessType.EXPORT)
    @PostMapping("/yijiaopack")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:yijiaopack')")
    public void yijiaopack(@RequestParam("id") Long id, HttpServletResponse response)throws IOException {
        handOverReceiveService.yijiaopack(id,response);
    }

    /**
     * 移交办结
     * @return
     */
    @Log(title = "移交办结", businessType = BusinessType.UPDATE)
    @PostMapping("/yijiaoBanjie/{id}")
    public R<Integer> yijiaoBanjie(@PathVariable("id") Long id){
        int i = handOverReceiveService.yijiaoBanjie(id);
        return R.ok(Integer.valueOf(i));
    }

    /**
     * 标记接收
     * @param id
     * @return
     */
    @Log(title = "标记接收", businessType = BusinessType.UPDATE)
    @PostMapping("/jsState")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:jsState')")
    public AjaxResult jsState(@RequestParam("id") Long id){
        handOverReceiveService.jsState(id, SecurityUtils.getTenantCode());
        return AjaxResult.success();
    }

    /**
     * 标记接收
     * @param id
     * @return
     */
    @Log(title = "标记接收", businessType = BusinessType.UPDATE)
    @PostMapping("/jsState1")
    @PreAuthorize("@ss.hasPermi('archive:yijiaoDan:jsState1')")
    public AjaxResult jsState1(@RequestParam("id") Long id){
        handOverReceiveService.jsState(id, SecurityUtils.getTenantCode());
        return AjaxResult.success();
    }

    /**
     * 审批流程不同意
     * @param id 移交单ID
     * @return
     */
    @Log(title = "审批流程不同意", businessType = BusinessType.UPDATE)
    @PostMapping("/killHOV/{id}")
    public AjaxResult killHOV(@PathVariable("id") Long id){
        handOverReceiveService.killHOV(id);
        return AjaxResult.success();
    }

    @Log(title = "在线推送", businessType = BusinessType.SELECT)
    @PostMapping("/onlinpush")
    public AjaxResult onlinpush(@RequestParam("ids") Long [] ids) {
        return AjaxResult.success(handOverReceiveService.onlinpush(ids));
    }

}
